//
//  SHEHomeListViewC.m
//  SHEPuzzleFigure
//
//  Created by SW mini on 2019/11/25.
//  Copyright © 2019 Figure. All rights reserved.
//

#import "SHEHomeListViewC.h"
#import "SHEHomeListCell.h"
#import "SHEViewController.h"


@interface SHEHomeListViewC ()<UICollectionViewDelegate,UICollectionViewDataSource>

//@property (strong, nonatomic) UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property (strong, nonatomic) NSMutableArray *cellArray;

@end

@implementation SHEHomeListViewC

- (void)viewDidLoad {
    [super viewDidLoad];
    [super setTitleView:self.titleStr];
    [super createBackBtn];
    
    [self collectionViewConfiguration];
    [self getWithCollectionData];
    
    //MJ
    self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self refreshMore];
    }];
    [self.collectionView.mj_footer endRefreshingWithNoMoreData];
}
- (void)refreshMore{
    
}

- (NSMutableArray *)cellArray{
    if (_cellArray == nil) {
        _cellArray = [NSMutableArray arrayWithCapacity:1];
    }
    return _cellArray;
}

- (void)getWithCollectionData{
    [EasyLoadingView showLoadingText:@"加载中..."];
    [SHENetworkObj postwithUrl:API_LIST success:^(NSDictionary * _Nullable dict) {
        NSString *state = [NSString stringWithFormat:@"%@",dict[@"code"]];
        if ([state isEqualToString:@"0"]) {
            self.cellArray = dict[@"data"];
            [self.collectionView reloadData];
        }else{
            [super PromptInformation:dict[@"msg"]];
        }
    } failure:^(NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
    }];
}


#pragma mark - 创建collectionView并设置代理
- (void)collectionViewConfiguration{
    //注册cell
    [_collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([SHEHomeListCell class]) bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"SHEHomeListCell"];
    //自适应大小
    _collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((SHEWidth-20)/2, (SHEWidth-20)/2);
}

#pragma mark - UICollectionView delegate dataSource
#pragma mark 定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [_cellArray count];
}

#pragma mark 定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

#pragma mark 每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    SHEHomeListCell *cell = (SHEHomeListCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"SHEHomeListCell" forIndexPath:indexPath];
    [cell sizeToFit];
    
    cell.homeDict = self.cellArray[indexPath.row];
    
    
    return cell;
}


#pragma mark UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    SHEViewController *VC = [[SHEViewController alloc] init];
    VC.detailsDcit = self.cellArray[indexPath.row];
    if ([self.titleStr isEqualToString:@"简单"]) {
        VC.difficultyInt = 3;
    }else if([self.titleStr isEqualToString:@"正常"]){
        VC.difficultyInt = 4;
    }else{
        VC.difficultyInt = 5;
    }
    VC.titleStr = self.titleStr;
    [self.navigationController pushViewController:VC animated:YES];
}



/*
 
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
